package com.woniu.micropropertyserver.mapper;


import com.woniu.microcommonsserver.entity.RelativesInfoEntity;
//import com.woniu.micropropertyserver.bean.RelativesInfo;
import com.woniu.micropropertyserver.util.PageRest;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RelativesInfoMapper {

    @ResultType(RelativesInfoEntity.class)
    @Select("select id,name,sex,card_type cardType,card_id cardId,phone,item,house_num houseNum,headimg, fk_owner_id fkOwnerId,relatives from relatives_info where status=0")
    List<RelativesInfoEntity> findall();

    List<RelativesInfoEntity> findAllByParams(PageRest pageRest, @Param("P") RelativesInfoEntity relativesInfo);

    @ResultType(RelativesInfoEntity.class)
    @Select("select id,name,sex,card_type cardType,card_id cardId,phone,item,house_num houseNum,headimg, fk_owner_id fkOwnerId,relatives from relatives_info")
    RelativesInfoEntity findById(Long id);

    @ResultType(RelativesInfoEntity.class)
    @Select("select id,name,sex,card_type cardType,card_id cardId,phone,item,house_num houseNum,headimg, fk_owner_id fkOwnerId,relatives from relatives_info where fk_owner_id=#{id}")
    List<RelativesInfoEntity> findByOwner(Long id);

    @Update("<script>" +
            " update relatives_info" +
            "<set>" +
            "<if test='o.name!=null'>" +
            "name=#{o.name}," +
            "</if>" +
            "<if test='o.sex!=null'>" +
            "sex=#{o.sex}," +
            "</if>" +
            "<if test='o.cardType!=null'>" +
            "card_type=#{o.cardType}," +
            "</if>" +
            "<if test='o.cardId!=null '>" +
            "card_id=#{o.cardId}," +
            "</if>" +
            "<if test='o.houseNum!=null'>" +
            "house_num=#{o.houseNum}," +
            "</if>" +
            "<if test='o.item!=null '>" +
            "item=#{o.item}," +
            "</if>" +
            "<if test='o.headimg!=null'>" +
            "headimg=#{o.headimg}," +
            "</if>" +
            "<if test='o.fkOwnerId!=null '>" +
            "fk_owner_id=#{o.fkOwnerId}," +
            "</if>" +
            "<if test='o.relatives!=null'>" +
            "relatives=#{o.relatives}," +
            "</if>" +
             "</set>where id=#{o.id}" +
            "</script>")

    int updateInfo(@Param("o") RelativesInfoEntity relativesInfo);

    @Delete("delete from relatives_info where id=#{id}")
    int delInfo(Long id);

    @Options(
            useGeneratedKeys = true,
            keyProperty = "r.id"
    )
     @Insert("insert into relatives_info(id,name,sex,card_type,card_id,phone,item,house_num,headimg, fk_owner_id ,relatives,status )values(null,#{r.name},#{r.sex},#{r.cardType},#{r.cardId},#{r.phone},#{r.item},#{r.houseNum},#{r.headimg},#{r.fkOwnerId} ,#{r.relatives},0 ) ")
    Long saveInfo(@Param("r") RelativesInfoEntity relativesInfo);

    @ResultType(RelativesInfoEntity.class)
    @Select("select id,name,card_id cardId from relatives_info where card_id=#{id} ")
    RelativesInfoEntity findByCard(String id);

}
